﻿using System;
using System.Collections.Generic;

namespace UniRx.Operators
{
	// Token: 0x0200032F RID: 815
	internal class ToArrayObservable<TSource> : OperatorObservableBase<TSource[]>
	{
		// Token: 0x060010ED RID: 4333 RVA: 0x0004E014 File Offset: 0x0004C414
		public ToArrayObservable(IObservable<TSource> source) : base(source.IsRequiredSubscribeOnCurrentThread<TSource>())
		{
			this.source = source;
		}

		// Token: 0x060010EE RID: 4334 RVA: 0x0004E029 File Offset: 0x0004C429
		protected override IDisposable SubscribeCore(IObserver<TSource[]> observer, IDisposable cancel)
		{
			return this.source.Subscribe(new ToArrayObservable<TSource>.ToArray(observer, cancel));
		}

		// Token: 0x040009FA RID: 2554
		private readonly IObservable<TSource> source;

		// Token: 0x02000330 RID: 816
		private class ToArray : OperatorObserverBase<TSource, TSource[]>
		{
			// Token: 0x060010EF RID: 4335 RVA: 0x0004E03D File Offset: 0x0004C43D
			public ToArray(IObserver<TSource[]> observer, IDisposable cancel) : base(observer, cancel)
			{
			}

			// Token: 0x060010F0 RID: 4336 RVA: 0x0004E054 File Offset: 0x0004C454
			public override void OnNext(TSource value)
			{
				try
				{
					this.list.Add(value);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x060010F1 RID: 4337 RVA: 0x0004E0B0 File Offset: 0x0004C4B0
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x060010F2 RID: 4338 RVA: 0x0004E0E8 File Offset: 0x0004C4E8
			public override void OnCompleted()
			{
				TSource[] value;
				try
				{
					value = this.list.ToArray();
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				this.observer.OnNext(value);
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x040009FB RID: 2555
			private readonly List<TSource> list = new List<TSource>();
		}
	}
}
